// pages/productRecom/productRecom.js
import mode from "../../utils/mode.js"
Page({

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage () {   //分享好友
    //     this.setData({
    //         isShare:1,
    //     })
    //     return {
    //         title:this.data.shareText,
    //         path: `/pages/shareIndex/shareIndex?share_id=${this.data.userMessage.id}&userName=${this.data.userMessage.user_nickname}`,
    //         imageUrl: 'https://2am-project.cn/img/20220524/shareCard.png',   //分享图片
    //     }
    // },

    /**
     * 页面的初始数据
     */
    data: {
        shareText:'邀您开启精准护肤之旅',
        yijianList: [],
        addList: [],
        list: [],
        oneIdx: 0,
        twoIdx: 0,
        threeIdx: 0,
        fourIdx: 0,
        fiveIdx: 0,
        sixIdx: 0,
        top: {},
        goodList: [],
        temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg','IKdsAyES45qURTGEQMaHqLRq-fpoaU83kNRtz3TcC78'],
        isShare:0,  //是否进行分享
        showShare:false,  //分享成功展示
    },
    handleSwiperChange (e) {
        let idx = e.currentTarget.dataset.idx
        if (idx == 1) {
            this.setData({
                oneIdx: e.detail.current
            })
        } else if (idx == 2) {
            this.setData({
                twoIdx: e.detail.current
            })
        } else if (idx == 3) {
            this.setData({
                threeIdx: e.detail.current
            })
        } else if (idx == 4) {
            this.setData({
                fourIdx: e.detail.current
            })
        } else if (idx == 5) {
            this.setData({
                fiveIdx: e.detail.current
            })
        } else {
            this.setData({
                sixIdx: e.detail.current
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('options', options)
        let that = this
        that.setData({
            id: options.user_id,
        })
        that.getDetail(options)
    },
    setInfo(){// 订阅消息
        let that = this
        this.setData({
          showShare:false,
          isShare:0
        })
        let shareData={
          user_id:that.data.userMessage.id,
          type:3,
          other_type:'',
        }
        wx.requestSubscribeMessage({
            tmplIds: that.data.temIds,
            success (res) {
              let arr = []
                if(res[that.data.temIds[0]]=='accept'){//拒绝
                  arr.push('1')
                }
                if(res[that.data.temIds[1]]=='accept'){
                  arr.push('2')
                }
                // else if(res[that.data.temIds[2]]=='accept'){
                  // arr.push('3')
                // }else if(res[that.data.temIds[3]]=='accept'){  //允许
                  // arr.push('4')
                // }
                if(arr.length>0){
                  shareData.other_type = arr.join(',')
                  that.sendSetYue(shareData)
                }
            },
            fail(error){
              console.log(error,'123失败');
            }
          })
      },
      sendSetYue(data){
        wx.$api.sendTemplate(data).then(res=>{
                if(res.code>0){
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
                }
        })
    },
    getDetail(options){
        let that = this
        mode.getReportDetail(options.report_id).then(resData => {
            console.log('item', resData)
            let item = resData.data.report
            that.setData({
                top: item
            })
            if (!item.goods) {
                return
            }
            let addList = []
            let yijianList = []
            if (item.goods.Cleanser) {
                that.data.goodList.push(item.goods.Cleanser)
                addList.push(item.goods.Cleanser)
            }
            if (item.goods.Eyes) {
                that.data.goodList.push(item.goods.Eyes)
                addList.push(item.goods.Eyes)
            }
            if (item.goods.Serums) {
                that.data.goodList.push(item.goods.Serums)
                addList.push(item.goods.Serums)
            }
            if (item.goods.Toner) {
                that.data.goodList.push(item.goods.Toner)
                addList.push(item.goods.Toner)
            }
            if (item.goods.moisturizer) {
                that.data.goodList.push(item.goods.moisturizer)
                addList.push(item.goods.moisturizer)
            }
            if (item.goods.Sunscreen) {
                that.data.goodList.push(item.goods.Sunscreen)
                addList.push(item.goods.Sunscreen)
            }
            for (let i = 0; i < addList.length; i++) {
                if (Array.isArray(addList[i]) && addList[i].length > 0) {
                    for (let j = 0; j < addList[i].length; j++) {
                        if (addList[i][j].goods_no) {
                            addList[i][j].isFlag = true
                            // yijianList.push(addList[i][j])
                            let item = {
                                goods_id: addList[i][j].id,
                                number: 1
                            }
                            yijianList.push(item)
                        }
                    }
                }

            }
            wx.setStorageSync('top', options.list)
            that.setData({
                goodList: that.data.goodList,
                yijianList: yijianList,
                list: item.goods
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    handleClickShare(){
        wx.navigateTo({
          url: '/pages/sharePage/sharePage',
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var uinfo = wx.getStorageSync('uinfo')
        let n = uinfo.user.user_nickname || ''
        let title =  n + '邀您开启精准护肤之旅'
        this.setData({
            shareText: title,
            userMessage: uinfo.user
        })
        // if(this.data.isShare>0){
        //     this.setData({
        //         showShare:true
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    async addCart (e) {
        let goodInfo = e.target.dataset.index
        let arr = []
        let good = {
            goods_id: goodInfo.id,
            number: 1
        }
        arr.push(good)
        let postData = JSON.stringify(arr)
        let { code, msg } = await mode.cartAdd({
            user_id: this.data.id,
            goods_info: postData
        })
        if (code == 1) {
            wx.showToast({ title: '商品加入购物车成功！', icon: 'none', mask: true })
        } else {
            wx.showToast({ title: msg, icon: 'none' })
        }
    },

    //一键加购
    async allInCart () {
        let list = JSON.stringify(this.data.yijianList)
        let { code, msg } = await mode.cartAdd({
            user_id: this.data.id,
            goods_info: list,
        })
        if (code == 1) {
            wx.showToast({ title: '一键加购商品成功！', icon: 'none', mask: true })
            setTimeout(() => {
                wx.switchTab({
                    url: '/pages/exclusive/exclusive',
                })
            }, 1500)
        } else {
            wx.showToast({ title: msg, icon: 'none' })
        }

    }
})